import {
	getListConstruction,
	getDetailConstruction,
	addConstruction,
	updateConstuction,
	deleteConstruction,
	getListConvenience,
	updateConvenience,
	getListEvent,
	getListPark,
	updatePark,
	dateExist,
} from './service';

export default {
	namespace: 'data',
	state: {
		constructionList: {
			pageData: [
				{
					id: 1,
					dataTime: '2018-12-13',
					physicalExamNum: 100,
					skillsTrainNum: 132,
					moveInNum: 12,
					moveOutNum: 213,
					birthNum: 12,
					deathNum: 123,
					infirmNum: 1,
					lowIncomeNum: 12,
					caressesObjectNum: 2,
					olderNum: 99,
					disabilityNum: 135,
				},
			],
			pageSize: 10,
			pageNum: 1,
			total: 20,
		},
		convenienceList: {
			pageData: [],
			pageSize: 10,
			pageNum: 1,
			total: 20,
		},
		eventsList: {
			pageData: [],
			pageSize: 10,
			pageNum: 1,
			total: 20,
		},
		constructionDetail: {},
		constructionSearch: {
			pageSize: 10,
			pageNum: 1,
			startDate: '',
			endDate: '',
		},
		convenienceSearch: {
			pageSize: 10,
			pageNum: 1,
			startDate: '',
			endDate: '',
			name: '',
		},
		eventSearch: {
			pageSize: 10,
			pageNum: 1,
			startDate: '',
			endDate: '',
		},
		parkList: {
			pageData: [],
			pageSize: 10,
			pageNum: 1,
			total: 20,
		},
	},
	reducers: {
		save(state, { payload }) {
			return { ...state, ...payload };
		},
	},
	effects: {
		*getListConstruction({}, { call, put, select }) {
			const { pageSize, pageNum, startDate, endDate } = yield select(
				state => state.data.constructionSearch
			);
			const res = yield call(getListConstruction, { pageSize, pageNum, startDate, endDate });
			yield put({
				type: 'save',
				payload: {
					constructionList: {
						pageData: res.pageData,
						pageNum: res.pageNum,
						pageSize: res.pageSize,
						total: res.total,
					},
				},
			});
		},
		*getDetailConstruction({ payload }, { call, put }) {
			const data = yield call(getDetailConstruction, payload);
			yield put({
				type: 'save',
				payload: {
					constructionDetail: data,
				},
			});
		},
		*addConstruction({ payload, callback }, { call, put }) {
			yield call(addConstruction, payload);
			if (callback) callback();
		},
		*updateConstuction({ payload, callback }, { call }) {
			yield call(updateConstuction, payload);
			if (callback) callback();
		},
		*deleteConstruction({ payload, callback }, { call }) {
			yield call(deleteConstruction, payload);
			if (callback) callback();
		},
		*getListConvenience({}, { call, put, select }) {
			const { pageNum, pageSize, startDate, endDate, name } = yield select(
				state => state.data.convenienceSearch
			);
			const res = yield call(getListConvenience, {
				pageNum,
				pageSize,
				startDate,
				endDate,
				name,
			});
			yield put({
				type: 'save',
				payload: {
					convenienceList: {
						pageData: res.pageData,
						pageSize: res.pageSize,
						pageNum: res.pageNum,
						total: res.total,
					},
				},
			});
		},
		*updateConvenience({ payload, callback }, { call }) {
			yield call(updateConvenience, payload);
			if (callback) callback();
		},
		*getListEvent({}, { call, put, select }) {
			const { pageSize, pageNum, startDate, endDate } = yield select(
				state => state.data.eventSearch
			);
			const res = yield call(getListEvent, { pageSize, pageNum, startDate, endDate });
			yield put({
				type: 'save',
				payload: {
					eventsList: {
						pageData: res.pageData,
						pageNum: res.pageNum,
						pageSize: res.pageSize,
						total: res.total,
					},
				},
			});
		},
		*getListPark({}, { call, put, select }) {
			const res = yield call(getListPark, { pageSize: 10, pageNum: 1 });
			yield put({
				type: 'save',
				payload: {
					parkList: {
						pageData: res.pageData,
						pageSize: res.pageSize,
						pageNum: res.pageNum,
						total: res.total,
					},
				},
			});
		},
		*updatePark({ payload, callback }, { call }) {
			yield call(updatePark, payload);
			if (callback) callback();
		},
		*dateExist({ payload, callback }, { call }) {
			const data = yield call(dateExist, payload);
			if (callback) callback(data);
		},
	},
};
